<?php

namespace App\Http\Controllers\Schedule\Merchant;

use App\Http\Controllers\BaseController;
use App\Service\Schedule\ScheduleHandleService;
use Illuminate\Http\Request;

class ScheduleHandleController extends BaseController
{
    /**
     * 单次
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function single(Request $request)
    {
        $this->validate($request, [
            'ruleId'  => 'required|integer',
            'date'    => 'required|date',
            'userIds' => 'required|array',
        ]);

        $ruleId  = $request->post('ruleId');
        $date    = $request->post('date');
        $userIds = $request->post('userIds');

        $date = (int)date('Ymd', strtotime($date));

        (new ScheduleHandleService($request->header('shopId')))->single($ruleId, $date, $userIds);

        return $this->success();
    }

    public function combine(Request $request)
    {
        $this->validate($request, [
            'ruleIds' => 'required|array',
            'date'    => 'required|date',
            'userIds' => 'required|array',
        ]);

        $ruleIds = $request->post('ruleIds');
        $date    = $request->post('date');
        $userIds = $request->post('userIds');

        $date = (int)date('Ymd', strtotime($date));

        (new ScheduleHandleService($request->header('shopId')))->combine($ruleIds, $date, $userIds);

        return $this->success();
    }

    public function cycle(Request $request)
    {
        $this->validate($request, [
            'ruleIds'   => 'required|array',
            'startDate' => 'required|date',
            'endDate'   => 'required|date',
            'userId'    => 'required|integer',
        ]);

        $ruleIds   = $request->post('ruleIds');
        $startDate = $request->post('startDate');
        $endDate   = $request->post('endDate');
        $userId    = $request->post('userId');

        $startDate = (int)date('Ymd', strtotime($startDate));
        $endDate   = (int)date('Ymd', strtotime($endDate));

        (new ScheduleHandleService($request->header('shopId')))->cycle($ruleIds, $startDate, $endDate, $userId);

        return $this->success();
    }

    public function del(Request $request)
    {
        $this->validate($request, [
            'date'    => 'required|date',
            'userIds' => 'required|array',
        ]);

        $date    = $request->post('date');
        $userIds = $request->post('userIds');

        $date = (int)date('Ymd', strtotime($date));

        (new ScheduleHandleService($request->header('shopId')))->del($date, $userIds);

        return $this->success();
    }
}
